import {
    orderDetail
} from '../../../api/myorder'
import Toast from 'tdesign-miniprogram/toast/index';
Page({
    data: {
        items: [null, null],
        orderId: '',
        order: {},
        timer: '',
        time: ''
    },

    onShareAppMessage() {
        return {};
    },
    copyOrderCode(e) {
        const {
            order
        } = e.currentTarget.dataset;
        wx.setClipboardData({
            data: order,
            success(res) {
                wx.getClipboardData({
                    success(res) {}
                })
            }
        })
    },
    async getOrderDetail() {
        const res = await orderDetail(this.data.orderId, true);
        console.log("payPrice", res);
        res.items.map(item => {
            item.payPrice = (item.payPrice / 100).toFixed(2);
            item.price = (item.price / 100).toFixed(2);
        })
        let delivery = ''
        let statusText = ''
        if (res.deliveryType == 1) {
            delivery = '快递发货'
        } else if (res.deliveryType == 2) {
            delivery = '用户自提'
        } else if (res.deliveryType == 31) {
            delivery = '骑士配送'
        }
        if (res.refundStatus > 0) {
            if (res.refundStatus == 10) {
                statusText = '售后-申请中'
            } else if (res.refundStatus == 20) {
                statusText = '售后-卖家通过'
            } else if (res.refundStatus == 30) {
                statusText = '售后-卖家通过'
            } else if (res.refundStatus == 40) {
                statusText = '售后-等待平台退款'
            } else if (res.refundStatus == 50) {
                statusText = '售后-完成'
            } else if (res.refundStatus == 61) {
                statusText = '售后-买家取消售后'
            } else if (res.refundStatus == 62) {
                statusText = '售后-卖家拒绝'
            } else if (res.refundStatus == 63) {
                statusText = '售后-卖家拒绝收货'
            }
        } else {
            if (res.status == 0) {
                statusText = '待付款'
            } else if (res.status == 10) {
                statusText = '待发货'
            } else if (res.status == 20) {
                statusText = '待发货'
            } else if (res.status == 30) {
                statusText = '已完成'
            } else if (res.status == 40) {
                statusText = '已取消'
            }
        }
        res.deliveryText = delivery
        res.statusText = statusText
        res.payPrice = (res.payPrice / 100).toFixed(2);
        res.totalPrice = (res.totalPrice / 100).toFixed(2);
        res.payTime = this.formatTime(res.payTime);
        this.countDown(res.payExpireTime, res.createTime)
        this.setData({
            order: res
        })
    },
    formatTime(time) {
        var date = new Date(time)
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = date.getDate() + ' ';
        return (Y + M + D)
    },
    countDown: function (endTime, createTime) {
        var that = this;
        that.setData({
            timer: setInterval(function () {
                var downTime = parseInt(endTime - createTime)
                // 倒计时结束
                if (downTime <= 0) {
                    //结束周期计时器
                    clearInterval(that.data.timer);
                    return;
                }
                //计算距离活动还有多少天、时、分、秒
                var d = parseInt(downTime / 1000 / 3600 / 24);
                var h = parseInt(downTime / 1000 / 3600 % 24);
                var m = parseInt(downTime / 1000 / 60 % 60);
                var s = parseInt(downTime / 1000 % 60);
                //统一格式的显示
                d < 10 ? d = '0' + d : d;
                h < 10 ? h = '0' + h : h;
                m < 10 ? m = '0' + m : m;
                s < 10 ? s = '0' + s : s;
                //同步显示
                that.setData({
                    time: m + ':' + s
                })
            }, 1000)
        })
    },
    onLoad(query) {
        const {
            id
        } = query;
        this.setData({
            orderId: id
        })
        this.getOrderDetail();
    }
});